<HTML>
<HEAD>
<TITLE>copy(), copy_backward()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Rogue Wave Standard Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="containers.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="count.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Rogue Wave C++ Standard Library Reference Guide</B></DIV>
<H2>copy(), copy_backward()</H2>
<P><B>Module:</B>&nbsp;&nbsp;Standard C++ Library&nbsp;&nbsp;&nbsp;<B>Library:</B>&nbsp;&nbsp;<A HREF="2-9.html">Algorithms</A></P>

<PRE><HR><B><I>Function</I></B><HR></PRE>

<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Complexity</A></LI>
<LI><A HREF="#sec6">Example</A></LI>
<LI><A HREF="#sec7">See Also</A></LI>
<LI><A HREF="#sec8">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
No Entries
<A NAME="sec2"><H3>Summary</H3></A>
<P>Algorithm that copies a range of elements</P>
<A NAME="sec3"><H3>Synopsis</H3></A>

<PRE>#include &lt;algorithm&gt;

namespace std {
  template &lt;class InputIterator, class OutputIterator&gt;
  OutputIterator copy(InputIterator start, 
                      InputIterator finish,
                      OutputIterator result);
  template &lt;class BidirectionalIterator1, 
            class BidirectionalIterator2&gt;
  BidirectionalIterator2
  copy_backward(BidirectionalIterator1 start,
                BidirectionalIterator1 finish,
                BidirectionalIterator2 result);
}
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>The <SAMP>copy()</SAMP> algorithm copies values from the range specified by <SAMP>[start</SAMP>, <SAMP>finish)</SAMP> to the range specified by <SAMP>[result, result + (finish - start))</SAMP>. <SAMP>copy()</SAMP> returns <SAMP>result + (finish - start)</SAMP>. For each non-negative integer <SAMP>n &lt; (finish - start)</SAMP>, <SAMP>copy()</SAMP> assigns <SAMP>*(start + n)</SAMP> to <SAMP>*(result + n)</SAMP>. The result of <SAMP>copy()</SAMP> is undefined if <SAMP>result</SAMP> is in the range <SAMP>[start, finish)</SAMP>.</P>
<P>Unless <SAMP>result</SAMP> is an insert iterator, <SAMP>copy()</SAMP> assumes that all iterators in the range <SAMP>[result + (finish - start)]</SAMP> are dereferenceable.</P>
<P>The <SAMP><A HREF="copy.html">copy_backward()</A></SAMP> algorithm copies elements in the range specified by <SAMP>[start, finish)</SAMP> into the range specified by [<SAMP>result - (finish - start), result)</SAMP>, starting from the end of the sequence (<SAMP>finish-1</SAMP>) and progressing to the front (<SAMP>start</SAMP>). Note that <SAMP>copy_backward()</SAMP> does <I>not </I>reverse the order of the elements, it simply reverses the order of transfer. <SAMP>copy_backward()</SAMP> returns <SAMP>result - (finish - start)</SAMP>. You should use <SAMP>copy_backward()</SAMP> instead of <SAMP>copy()</SAMP> when <SAMP>finish</SAMP> is in the range <SAMP>[result - (finish - start), result)</SAMP>. For each positive integer <SAMP>n &lt;= (finish - start)</SAMP>, <SAMP>copy_backward()</SAMP> assigns <SAMP>*(finish - n)</SAMP> to <SAMP>*(result - n)</SAMP>. The result of <SAMP>copy_backward()</SAMP> is undefined if <SAMP>result</SAMP> is in the range <SAMP>[start, finish)</SAMP>. <SAMP>copy_backward()</SAMP> should be used when <SAMP>finish</SAMP> is in the range <SAMP>[result - (finish - start), result)</SAMP>.</P>
<P>Unless <SAMP>result</SAMP> is an insert iterator, <SAMP><A HREF="copy.html">copy_backward()</A></SAMP> assumes that all iterators in the range <SAMP>[result - (finish - start), result)</SAMP> are dereferenceable.</P>
<A NAME="sec5"><H3>Complexity</H3></A>
<P>Both <SAMP>copy()</SAMP> and <SAMP><A HREF="copy.html">copy_backward()</A></SAMP> perform exactly <SAMP>finish - start</SAMP> assignments.</P>
<A NAME="sec6"><H3>Example</H3></A>

<UL><PRE>//
// copyex.cpp
//

#include &lt;algorithm&gt;   // for copy
#include &lt;iostream&gt;    // for cout, endl, ostream_iterator
#include &lt;vector&gt;      // for vector



int main ()
{
    // Typedef for convenience.
    typedef std::vector&lt;short, std::allocator&lt;short&gt; &gt; vector;

    const vector::value_type d1[] = { 1, 2, 3, 4 };
    const vector::value_type d2[] = { 5, 6, 7, 8 };

    // Set up three vectors.
    vector v1(d1 + 0, d1 + 4), 
           v2(d2 + 0, d2 + 4), 
           v3(d2 + 0, d2 + 4);

    // Set up one empty vector.
    vector v4;

    // Copy v1 to v2.
    std::copy(v1.begin(), v1.end(), v2.begin());

    // Copy backwards v1 to v3.
    std::copy_backward(v1.begin(), v1.end(), v3.end());

    // Use insert iterator to copy into empty vector.
    std::copy(v1.begin(), v1.end(), std::back_inserter(v4));

    // Copy all four vectors to cout.
    std::ostream_iterator&lt;vector::value_type, char,
             std::char_traits&lt;char&gt; &gt; out(std::cout, " ");

    std::copy(v1.begin(), v1.end(), out);
    std::cout &lt;&lt; std::endl;

    std::copy(v2.begin(), v2.end(), out);
    std::cout &lt;&lt; std::endl;

    std::copy(v3.begin(), v3.end(), out);
    std::cout &lt;&lt; std::endl;

    std::copy(v4.begin(), v4.end(), out);
    std::cout &lt;&lt; std::endl;
   
    return 0;
}


Program Output:
</PRE></UL>
<UL><PRE>1 2 3 4 
1 2 3 4 
1 2 3 4 
1 2 3 4 
</PRE></UL>
<A NAME="sec7"><H3>See Also</H3></A>
<P><A HREF="algorithms.html">Algorithms</A>, <A HREF="algorithm-h.html">&lt;algorithm&gt;</A></P>
<A NAME="sec8"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.2.1</I></P>

<BR>
<HR>
<A HREF="containers.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="count.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A></BODY>
</HTML>
